package com.kede.app.service.impl;

import com.kede.app.domain.Users;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.UsersRepository;
import com.kede.app.service.UsersService;
import com.kede.app.service.dto.UsersDto;
import com.kede.app.service.dto.UsersQueryCriteria;
import com.kede.app.service.mapstruct.UsersMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-05-11
**/
@Service
@RequiredArgsConstructor
public class UsersServiceImpl implements UsersService {

    private final UsersRepository usersRepository;
    private final UsersMapper usersMapper;

    @Override
    public Map<String,Object> queryAll(UsersQueryCriteria criteria, Pageable pageable){
        Page<Users> page = usersRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(usersMapper::toDto));
    }

    @Override
    public List<UsersDto> queryAll(UsersQueryCriteria criteria){
        return usersMapper.toDto(usersRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public UsersDto findById(Integer id) {
        Users users = usersRepository.findById(id).orElseGet(Users::new);
        ValidationUtil.isNull(users.getId(),"Users","id",id);
        return usersMapper.toDto(users);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UsersDto create(Users resources) {
        return usersMapper.toDto(usersRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Users resources) {
        Users users = usersRepository.findById(resources.getId()).orElseGet(Users::new);
        ValidationUtil.isNull( users.getId(),"Users","id",resources.getId());
        users.copy(resources);
        usersRepository.save(users);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            usersRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<UsersDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UsersDto users : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("积分数量", users.getIntegral());
            map.put("1会员0非会员", users.getIsVip());
            map.put("1用户2骑手3门店4区代理5市代理", users.getUserType());
            map.put("用户昵称", users.getUsersName());
            map.put("用户头像", users.getUsersImage());
            map.put("用户性别", users.getUsersSex());
            map.put("用户电话", users.getUsersPhone());
            map.put("所属代理", users.getAdminId());
            map.put(" 唯一表示凭证", users.getOpenId());
            map.put("用户总收入", users.getCountMoney());
            map.put("用户已提现金额", users.getWithdrawMoney());
            map.put("用户当前余额", users.getResidueMoney());
            map.put("token", users.getToken());
            map.put("token过期时间", users.getExpirationTime());
            map.put("更新时间", users.getCreatetime());
            map.put("用户状态:1=正常 ,2=拉黑", users.getStatus());
            map.put("用户密码", users.getPassword());
            map.put("出生日期", users.getBirthday());
            map.put(" sex",  users.getSex());
            map.put(" openid",  users.getOpenid());
            map.put("小程序openid", users.getXcxOpenid());
            map.put("app骑手端openid", users.getAppRiderOpenid());
            map.put(" xcxRiderOpenid",  users.getXcxRiderOpenid());
            map.put("门店端小程序openid", users.getXcxStoreOpenid());
            map.put("小程序区代理", users.getXcxQuOpenid());
            map.put("小程序市代理openid", users.getXcxShiOpenid());
            map.put(" thumb",  users.getThumb());
            map.put("是否是新用户", users.getIsNew());
            map.put(" lastid",  users.getLastid());
            map.put("余额", users.getUserMoney());
            map.put(" wxQrcode",  users.getWxQrcode());
            map.put(" wxName",  users.getWxName());
            map.put(" alipayQrcode",  users.getAlipayQrcode());
            map.put(" alipayName",  users.getAlipayName());
            map.put("本月已兑换数量", users.getYetNum());
            map.put("会员到期时间", users.getExpireTime());
            map.put("支付密码", users.getPayPwd());
            map.put("省市区", users.getAddress());
            map.put("详细地址", users.getAddressDetail());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}